import pygame
from settings import *
from Athlete import *
from Button import *

import math
from game_stats import *
def drawScene(screen, ai_settings):
    backGround = pygame.image.load("../resourse/pic/BackGroud.bmp")
    screen.blit(backGround, (40, 70))   #原本y = 20


# Draw athlete
def drawAthlete(screen, ai_settings,stats, cureent_control, athlete_List):
    #Training mode: blue center, red and blue goalkeeper
    #Simple mode: red and blue center, red and blue left and right point guards, red and blue goalkeepers
    #Difficulty mode: red and blue center, red and blue left and right point guards, red and blue goalkeepers, red and blue left and right wingers

    # Draw the current controlling athlete
    pygame.draw.circle(screen, cureent_control.color, cureent_control.pos, cureent_control.radius, 0)

    CtLst = athlete_List[0]         #Center forward
    gtLst = athlete_List[1]         #goalkeeper
    if stats.simply or stats.hard:
        DfLst=athlete_List[2]           #Point guard
    if stats.hard:
        WgLst=athlete_List[3]           #winger

    # Draw goalkeeper
    for gt in gtLst:
        pygame.draw.circle(screen, gt.color, gt.pos, gt.radius, 0)

    #Training mode
    if stats.practice:
        #Draw blue center
        pygame.draw.circle(screen,CtLst[0].color,CtLst[0].pos,CtLst[0].radius,0)
    if stats.simply:
        #Draw red and blue center
        for ct in CtLst:
            pygame.draw.circle(screen, ct.color, ct.pos, ct.radius, 0)
            #Draw point guard
        for df in DfLst:
            pygame.draw.circle(screen, df.color, df.pos, df.radius, 0)
    if stats.hard:
        # Draw red and blue center
        for ct in CtLst:
            pygame.draw.circle(screen, ct.color, ct.pos, ct.radius, 0)
        # Draw point guard
        for df in DfLst:
            pygame.draw.circle(screen, df.color, df.pos, df.radius, 0)
        #绘制边锋
        for wg in WgLst:
            pygame.draw.circle(screen, wg.color, wg.pos, wg.radius, 0)


    # # 绘制中边锋
    # for ct in CtLst:
    #     pygame.draw.circle(screen, ct.color, ct.pos, ct.radius, 0)
    #
    # # 绘制守门员
    # for gt in gtLst:
    #     pygame.draw.circle(screen, gt.color, gt.pos, gt.radius, 0)


# 画球
def drawBall(screen, cureent_control, Ball):
    if cureent_control.keep == False:  # 为非持球状态
        eM = edgeMap()
        # 向右走

        if Ball.speedx > 0:
            if Ball.pos[0] + Ball.ax > eM.xMax and not (310 <= Ball.pos[1] <= 378) or (Ball.pos[0] + Ball.ax < eM.xMax and Ball.pos[0] + Ball.ax >1040 and (Ball.pos[1] - 1 <= 88 or Ball.pos[1] + 1 >= 598)):
                # 反弹机制
                # 如果已进入球门则不必反弹
                Ball.speedx -= cureent_control.speedx #动量守恒
                Ball.speedx *= -1
            else:
                Ball.pos[0] += Ball.ax
                Ball.speedx -= 1
        # 向左走
        elif Ball.speedx < 0:
            if Ball.pos[0] - Ball.ax < eM.xMin and not (310 <= Ball.pos[1] <= 378) or ( Ball.pos[0] - Ball.ax > eM.xMin and Ball.pos[0]+Ball.ax <= 155 and (Ball.pos[1] - 1 <= 88 or Ball.pos[1] + 1 >= 598 )):
                Ball.speedx -= cureent_control.speedx
                Ball.speedx *= -1
            else:
                Ball.pos[0] -= Ball.ax
                Ball.speedx += 1

        # 向下走
        if Ball.speedy > 0:
            if Ball.pos[1] + 1 >= eM.yMax or (Ball.pos[1] + 1 < eM.yMax and Ball.pos[1] + 1 >= 598 and Ball.pos[0]+Ball.ax <= 155):
                Ball.speedy -= cureent_control.speedy
                Ball.speedy *= -1
            else:
                Ball.pos[1] += 1
                Ball.speedy -= 1
        # 向上走
        elif Ball.speedy < 0:
            if Ball.pos[1] - 1 <= eM.yMin:
                Ball.speedy -= cureent_control.speedy
                Ball.speedy *= -1
            else:
                Ball.pos[1] -= 1
                Ball.speedy += 1

    else:
        # y=(r1+r2)*sin45°
        # x=(r1+r2)*cos45°
        # sin45°=根号内2/2
        solvePos = (cureent_control.radius + Ball.radius) * (math.sqrt(2) / 2)
        if cureent_control.moving_right == True:
            Ball.dR = True
        elif cureent_control.moving_left == True:
            Ball.dL = True

        if cureent_control.moving_up == True:
            Ball.dU = True
        elif cureent_control.moving_down == True:
            Ball.dD = True

        if Ball.dR:
            if Ball.dU:
                Ball.pos[0] = cureent_control.pos[0] + solvePos
                Ball.pos[1] = cureent_control.pos[1] - solvePos
            elif Ball.dD:
                Ball.pos[0] = cureent_control.pos[0] + solvePos
                Ball.pos[1] = cureent_control.pos[1] + solvePos
            else:
                Ball.pos[0] = cureent_control.pos[0] + cureent_control.radius + Ball.radius
                Ball.pos[1] = cureent_control.pos[1]

        elif Ball.dL:
            if Ball.dU:
                Ball.pos[0] = cureent_control.pos[0] - solvePos
                Ball.pos[1] = cureent_control.pos[1] - solvePos
            elif Ball.dD:
                Ball.pos[0] = cureent_control.pos[0] - solvePos
                Ball.pos[1] = cureent_control.pos[1] + solvePos
            else:
                Ball.pos[0] = cureent_control.pos[0] - cureent_control.radius - Ball.radius
                Ball.pos[1] = cureent_control.pos[1]

        if Ball.dU and not Ball.dR and not Ball.dL:
            Ball.pos[0] = cureent_control.pos[0]
            Ball.pos[1] = cureent_control.pos[1] - cureent_control.radius - Ball.radius

        elif Ball.dD and not Ball.dR and not Ball.dL:
            Ball.pos[0] = cureent_control.pos[0]
            Ball.pos[1] = cureent_control.pos[1] + cureent_control.radius + Ball.radius

    pygame.draw.circle(screen, Ball.color, Ball.pos, Ball.radius, 0)


# 开始按钮、难度选择按钮、暂停按钮、继续按钮、退出游戏按钮
# 难度设置：pratice(训练模式)、simply(简单模式)、hard(困难模式)
def creatButtons(ai_settings, screen,stats):
    White=(255,255,255)
    Blue = (0, 0, 255)
    Red = (255, 0, 0)
    Green = (0, 255, 0)
    Purple = (255, 0, 255)
    SkyBlue=(32,220,220)
    DeepGray=(110,110,110)
    ShallowGray=(179,179,179)
    play_button = Button(screen, ai_settings, "IceHockey Game!!!", ShallowGray,White, [400, 250], [400, 250])
    if stats.suspend ==False:
        s_c_button=control_button(screen,ai_settings,[1120,70])
    else:
        s_c_button = control_button(screen,ai_settings,[505,255])
    stop_button=control_button(screen,ai_settings,[1160,70])
    # suspend_button = Button(screen, ai_settings, "S!", White,Red,[1140,20],[60,60])
    # continue_button = Button(screen, ai_settings, "C!", White,Blue,[1140,20],[60,60])
    # stop_button = Button(screen, ai_settings, "E!", White,Red,[100,100],[60,60])

    # 创建难度选择按钮
    practice_button = Button(screen, ai_settings, "Practice", ShallowGray,Blue,[363,280],[150,150])
    simply_button = Button(screen, ai_settings, "Simply", ShallowGray,Green,[525,280],[150,150])
    hard_button = Button(screen, ai_settings, "Hard", ShallowGray,Purple,[690,280],[150,150])

    # Create the gameOver button
    gameOver=Button(screen,ai_settings,"Game Over",DeepGray,SkyBlue,[400, 250], [400, 250])

    buttons = [play_button, s_c_button, stop_button, practice_button, simply_button, hard_button,gameOver]
    return buttons
